﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows.Shapes;

namespace Hugo
{
    class CalibrateKinectUIPage : UIPage
    {

        private static int nextIconCanvasID;

        const int labelFontSize = 60;
        private Label currentSelectionLabel;
        private DispatcherTimer timeTimer;
        private Point CursorPosition;

        private int BUFFER = 150;


        public CalibrateKinectUIPage()
            : base("Calibrate", Util.getBitmapSource(Properties.Resources.mail))
        {
            currentSelectionLabel = new Label();

            currentSelectionLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

            currentSelectionLabel.Foreground = new SolidColorBrush(GlobalSettings.TextColor);
            currentSelectionLabel.FontSize = labelFontSize;
            PageCanvas.Children.Add(currentSelectionLabel);

            currentSelectionLabel.Content = Translate.translate("Hold your hand at a comfortable level and wait");

            //Canvas.SetTop(currentSelectionLabel, this.positionYBaseline);
            
            // above stuff
            Canvas.SetTop(currentSelectionLabel, GlobalSettings.EffectiveScreenMiddleY - Util.getFontHeightFromSize(labelFontSize) - GlobalSettings.CONTENT_VERTICAL_SPACE/2);
            Canvas.SetLeft(currentSelectionLabel, 100);

            targetrect = new Rectangle();
            targetrect.Fill = new SolidColorBrush(Colors.Red);
            targetrect.Width = GlobalSettings.EffectiveScreenMiddleX * 2;
            targetrect.Height = BUFFER;
            PageCanvas.Children.Add(targetrect);
            Canvas.SetLeft(targetrect, 0);
            Canvas.SetTop(targetrect, GlobalSettings.EffectiveScreenMiddleY - BUFFER / 2);

            this.areShiftButtonsVisible = false;
            this.areQuickButtonsVisible = false;
        }

        private static Action EmptyDelegate = delegate() { };
        private Rectangle targetrect;

        private const int HOLD_TIME = 3;
        private int countDown = HOLD_TIME;

        public void adjustHeightOfKinectSensor(Object sender, EventArgs e)
        {
            // need to rerender to get updated width
            currentSelectionLabel.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
            Canvas.SetLeft(currentSelectionLabel, GlobalSettings.EffectiveScreenMiddleX - currentSelectionLabel.ActualWidth / 2);

            if (this.CursorPosition.Y > Canvas.GetTop(targetrect) && this.CursorPosition.Y < Canvas.GetTop(targetrect) + targetrect.ActualHeight)
            {
                countDown--;
                targetrect.Fill = new SolidColorBrush(Colors.Green);

                if (countDown == 0)
                {
                    timeTimer.Stop();
                    MainWindow.popAllPages();
                }
                return;
            }
            
            countDown = HOLD_TIME;
            targetrect.Fill = new SolidColorBrush(Colors.Red);


            if (this.CursorPosition.Y > GlobalSettings.EffectiveScreenMiddleY)
            {
                KinectCursorController.getInstance().moveDown();
            }
            else
            {
                KinectCursorController.getInstance().moveUp();
            }
        }

        public override bool setCursorPosition(Point p)
        {
            this.CursorPosition = p;
            return false;
        }

        public override void setPage()
        {
            timeTimer = new DispatcherTimer();
            timeTimer.Interval = TimeSpan.FromSeconds(3);
            timeTimer.Tick += new EventHandler(adjustHeightOfKinectSensor);
            timeTimer.Start();

            adjustHeightOfKinectSensor(null, null);

            base.setPage();
        }

        public override void unSetPage()
        {
            timeTimer.Stop();

            base.unSetPage();
        }
    }
}
